package com.sav.common.localCatalog;

import java.util.ArrayList;
import java.util.List;

import com.sav.common.catalog.Category;
import com.sav.common.dao.CategoryDAO;

public class LocalCategory implements Comparable {
	
	private int idCategory;
	private LocalCategory parentCategory;
	private String name;
	private String description;
	private int orderNum;
	private List<LocalCategory> listSubCategory;
	
	
	public LocalCategory(Category category) {
		setIdCategory(category.getIdCategory());
		if (category.getIdParentCategory() == 0) setParentCategory(null);
		setName(category.getName());
		setDescription(category.getDescription());
		setOrderNum(category.getOrderNum());
		
		CategoryDAO categoryDAO = new CategoryDAO();
		List<Category> list = categoryDAO.getListSubCategory(idCategory);
		listSubCategory = new ArrayList<LocalCategory>();
		for (Category c: list) {
			listSubCategory.add(new LocalCategory(c));
		}
		
		for (LocalCategory lc: listSubCategory) {
			lc.setParentCategory(this);
		}
	}
	
	public LocalCategory(String name, String description, LocalCategory parent, int orderNum) {
		setName(name);
		setDescription(description);
		setParentCategory(parent);
		int maxid = Catalog.getMaxID();
		setIdCategory(++maxid);
		setOrderNum(orderNum);
		Catalog.setMaxId(maxid);
		listSubCategory = new ArrayList<LocalCategory>();
	}
	
	public int getIdCategory() {
		return idCategory;
	}
	
	public void setIdCategory(int id) {
		this.idCategory = id;
	}
	
	public LocalCategory getParentCategory() {
		return parentCategory;
	}
	
	public void setParentCategory(LocalCategory parentCategory) {
		this.parentCategory = parentCategory;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public int getOrderNum() {
		return orderNum;
	}
	
	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}
	
	public List<LocalCategory> getListSubCategory() {
		return listSubCategory;
	}
	
	public void setListSubCategory(List<LocalCategory> listSubCategory) {
		this.listSubCategory = listSubCategory;
	}
	
	public Category toCategory() {
		int idParentCategory = 0;
		if (getParentCategory() != null) getParentCategory().getIdCategory();
		return new Category(
				getIdCategory(), 
				idParentCategory,
				getName(), 
				getDescription(), 
				getOrderNum());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + idCategory;
		result = prime * result
				+ ((listSubCategory == null) ? 0 : listSubCategory.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + orderNum;
		result = prime * result
				+ ((parentCategory == null) ? 0 : parentCategory.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LocalCategory other = (LocalCategory) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (idCategory != other.idCategory)
			return false;
		if (listSubCategory == null) {
			if (other.listSubCategory != null)
				return false;
		} else if (!listSubCategory.equals(other.listSubCategory))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (orderNum != other.orderNum)
			return false;
		if (parentCategory == null) {
			if (other.parentCategory != null)
				return false;
		} else if (!parentCategory.equals(other.parentCategory))
			return false;
		return true;
	}

	@Override
	public int compareTo(Object o) {
		if (orderNum < ((LocalCategory)o).getOrderNum()) return -1;
		if (orderNum > ((LocalCategory)o).getOrderNum()) return 1;
		return 0;
	}
}
